import sys
import json
from person import Person
from lift import Lift
from staircase import Staircase

# main code
def main(argv):
    # possible states:
    # 0 = entry; 1 = staircase; 2 = lift waiting; 3 = lift; 4 = lift exiting; 5 = cashier

    # load configuraion of simulation
    simulationparams = json.loads(open("simulationparams.json").read());
    # duration of an interation
    timestep = int(simulationparams["timestep"])
    # influx of persons per timestep
    influx = int(simulationparams["influx"])
    # ratio of people who use the (better alternative) staircase out of 10 people
    ratio = int(simulationparams["peoplewhousestaircaseperten"])
    # amount of people a staircase processes per duration
    staircasecapacity = int(simulationparams["staircasecapacity"])
    # amount of peiple a lift processes per duration
    liftcapacity = int(simulationparams["liftcapacity"])
    # simulation length
    simulationlength = int(simulationparams["simulationlength"])

    # simulation variables
    idoffset = 1
    people = []
    peoplehappy = 0
    progress = 0

    # create means of transporting those fatasses
    lift = Lift(liftcapacity)
    staircase = Staircase(staircasecapacity)

    # simulation
    while progress < simulationlength:
        # let the new people come in
        for i in range(influx):
            people.append(Person(idoffset + i))

        # take care of the id of our people
        idoffset += influx

        # let the means of transport do their thing
        print("staircase handling people")
        staircase.handle()
        print("lift handling people")
        lift.handle()

        for person in people:
            # at stage 0, the person must decide whether to take the lift or staircase
            if person.state == 0:
                # ratio out of 10 people use the staircase
                if person.id % 10 < ratio:
                    # queue the person for the staircase
                    staircase.addperson(person)
                else:
                    # queue the person for the lift
                    lift.addperson(person)
            elif person.state == 5:
                people.remove(person)
                peoplehappy += 1

        # now that a duration timestep is over increase progress
        progress += timestep
        print("+------+ TIMESTEP +------+")

    print(peoplehappy)
    print(len(people))

if __name__ == "__main__":
   main(sys.argv)
